package com.zlh123.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zlh123.commom.util.PageResult;
import com.zlh123.entity.Classes;
import com.zlh123.entity.Student;
import com.zlh123.mapper.StudentMapper;
import com.zlh123.service.IClassesService;
import com.zlh123.service.IStudentService;
import com.zlh123.vo.StudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zlh
 * @since 2025-01-07
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {
    @Autowired
    private IClassesService classesService;
    @Override
    public PageResult<Student> findByPage(int page, int pageSize) {
        //开始分页
        Page<Student> page1=this.page(new Page<>(page, pageSize));
        //分页结果
        return new PageResult<>(page1.getRecords(),page1.getTotal());
    }
//条件查询
    @Override
    public PageResult<Student> search(int page, int pageSize, StudentVo studentVo) {
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(studentVo.getSname()), "sname",  studentVo.getSname() )
                .like(StrUtil.isNotBlank(studentVo.getAddr()), "addr", studentVo.getAddr())
                .eq( studentVo.getCid() != 0, "cid", studentVo.getCid());
        //开始分页查询
        Page<Student> page1 = this.page(new Page<>(page, pageSize), wrapper);
        //处理学生所在班级名称
        page1.getRecords().forEach(st -> {
            Classes classes = classesService.getById(st.getCid());
            st.setCname(classes.getCname());
        });
        //封装分页结果
        return new PageResult<>(page1.getRecords(),page1.getTotal());
    }
//添加学生
    @Override
    public boolean add(Student student) {
        return this.save(student);
    }
}
